package com.example.lib_widget.titlebar;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.example.lib_widget.R;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yaotianxue
 * @description
 * @date 2024/10/25
 */
public class XiGuaTitleBar extends LinearLayout {
    private VerticalTextView textView;

    public XiGuaTitleBar(Context context) {
        super(context);
        init(context);
    }

    public XiGuaTitleBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    public XiGuaTitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    public XiGuaTitleBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    /**
     * 加载组合view
     * @param context
     */
    public void init(Context context){
        removeAllViews();
        View view = LayoutInflater.from(context).inflate(R.layout.view_xiguatitlebar, null);
        textView = view.findViewById(R.id.vt_view_xiguatitlebar_txt);
        addView(view);

        textView.setText(19.0f, 5, Color.LTGRAY); //设置属性,具体跟踪源码
        textView.setTextStillTime(3000); //设置停留时长间隔
        textView.setAnimTime(300);//设置进入和退出的时间间隔
        textView.setOnItemClickListener(new VerticalTextView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if(callback != null){
                    callback.onClick(position);
                }
            }
        });

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        textView.startAutoScroll();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        textView.stopAutoScroll();
    }
    /**
     * 设置数据源
     */
    public void setTextList(List<String> titles){
        textView.setTextList(titles);
    }

    /**
     * 设置事件
     */
    private TitleBarClickCallback callback;
    public void setTitleBarClickCallback(TitleBarClickCallback callback){
        this.callback = callback;
    }
    public interface TitleBarClickCallback{
        void onClick(int index);
    }
}
